<?php
namespace app\shop\controller;

use think\Loader;
use think\Request;
use think\Controller;
use app\shop\model\MemberOrder;
use app\shop\model\AuctionOrder as AuctionOrderModel;
use app\shop\model\MemberWithdraw as MemberWithdrawModel;

class Alipay extends Controller
{
    public function pay()
    {
        if (Request::instance()->isPost()) {
            header("Content-type: text/html; charset=utf-8");
            Loader::import('alipay.pagepay.service.AlipayTradeService');
            Loader::import('alipay.pagepay.buildermodel.AlipayTradePagePayContentBuilder');
            Loader::import('alipay.config');

            global $config;
            $out_trade_no = Request::instance()->param('order_number');
            if (!empty($out_trade_no) && trim($out_trade_no) != "") {

                //订单名称，必填
                $subject = 'Ishop支付';

                //付款金额，必填
                $price_fee = (new MemberOrder)->where('order_number|shop_order_number',$out_trade_no)->sum('price');
                $freight_fee = (new MemberOrder)->where('order_number|shop_order_number',$out_trade_no)->sum('freight');
                $total_amount = bcadd($price_fee,$freight_fee,2);

                //商品描述，可空
                $body = Request::instance()->post('body','');

                //构造参数
                $payRequestBuilder = new \AlipayTradePagePayContentBuilder();
                $payRequestBuilder->setBody($body);
                $payRequestBuilder->setSubject($subject);
                $payRequestBuilder->setTotalAmount($total_amount);
                $payRequestBuilder->setOutTradeNo($out_trade_no);

                $aop = new \AlipayTradeService($config);

                /**
                 * pagePay 电脑网站支付请求
                 * @param $builder 业务参数，使用buildmodel中的对象生成。
                 * @param $return_url 同步跳转地址，公网可以访问
                 * @param $notify_url 异步通知地址，公网可以访问
                 * @return $response 支付宝返回的信息
                 */

                $config['return_url'] = request()->domain() . '/index.php/shop//member_order/index';
                $config['notify_url'] = request()->domain() . '/index.php/interfaces/Alipay/notifyurl';


                $response = $aop->pagePay($payRequestBuilder,$config['return_url'],$config['notify_url']);

                //输出表单
                var_dump($response);
            }
        }
    }




    public function rechage()
    {
        header("Content-type: text/html; charset=utf-8");
        Loader::import('alipay.pagepay.service.AlipayTradeService');
        Loader::import('alipay.pagepay.buildermodel.AlipayTradePagePayContentBuilder');
        Loader::import('alipay.config');

        global $config;
        $out_trade_no = Request::instance()->post('order_number');
        $price = Request::instance()->post('price');
        if (!empty($out_trade_no) && trim($out_trade_no) != "") {

            //订单名称，必填
            $subject = 'Ishop支付';

            //付款金额，必填

            $total_amount =$price;

            //商品描述，可空
            $body = Request::instance()->post('body','');

            //构造参数
            $payRequestBuilder = new \AlipayTradePagePayContentBuilder();
            $payRequestBuilder->setBody($body);
            $payRequestBuilder->setSubject($subject);
            $payRequestBuilder->setTotalAmount($total_amount);
            $payRequestBuilder->setOutTradeNo($out_trade_no);

            $aop = new \AlipayTradeService($config);

            /**
             * pagePay 电脑网站支付请求
             * @param $builder 业务参数，使用buildmodel中的对象生成。
             * @param $return_url 同步跳转地址，公网可以访问
             * @param $notify_url 异步通知地址，公网可以访问
             * @return $response 支付宝返回的信息
             */

            $config['return_url'] = request()->domain() . '/index.php/shop//member_wallet/index';
            $config['notify_url'] = request()->domain() . '/index.php/interfaces/Alipay/recharge';


            $response = $aop->pagePay($payRequestBuilder,$config['return_url'],$config['notify_url']);

            //输出表单
            var_dump($response);
        }
    }


    public function auction()
    {
        header("Content-type: text/html; charset=utf-8");
        Loader::import('alipay.pagepay.service.AlipayTradeService');
        Loader::import('alipay.pagepay.buildermodel.AlipayTradePagePayContentBuilder');
        Loader::import('alipay.config');

        global $config;
        $out_trade_no = Request::instance()->post('order_number');

        if (!empty($out_trade_no) && trim($out_trade_no) != "") {

            //订单名称，必填
            $subject = 'Ishop支付';

            //付款金额，必填

            $total_amount = (new AuctionOrderModel)->where('order_number',$out_trade_no)->value('price');

            //商品描述，可空
            $body = Request::instance()->post('body','');

            //构造参数
            $payRequestBuilder = new \AlipayTradePagePayContentBuilder();
            $payRequestBuilder->setBody($body);
            $payRequestBuilder->setSubject($subject);
            $payRequestBuilder->setTotalAmount($total_amount);
            $payRequestBuilder->setOutTradeNo($out_trade_no);

            $aop = new \AlipayTradeService($config);

            /**
             * pagePay 电脑网站支付请求
             * @param $builder 业务参数，使用buildmodel中的对象生成。
             * @param $return_url 同步跳转地址，公网可以访问
             * @param $notify_url 异步通知地址，公网可以访问
             * @return $response 支付宝返回的信息
             */

            $config['return_url'] = request()->domain() . '/index.php/shop//member_wallet/auction';
            $config['notify_url'] = request()->domain() . '/index.php/interfaces/Alipay/auction';


            $response = $aop->pagePay($payRequestBuilder,$config['return_url'],$config['notify_url']);

            //输出表单
            var_dump($response);
        }
    }
}